<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams">
      <el-form-item label="随访模板名称" prop="followTempName">
        <el-input
          v-model="queryParams.followTempName"
          clearable
          placeholder="请输入随访模板名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['research:followTemp:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['research:followTemp:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['research:followTemp:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="followTempList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column v-if="false" align="center" label="随访模板ID" prop="followTempId"/>
      <el-table-column align="center" label="随访模板名称" prop="followTempName"/>
      <el-table-column align="center" label="间隔时间">
        <template slot-scope="scope">
          <span>{{ scope.row.intervalTime }}{{ timeTypeFormat(scope.row.intervalUnit) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="随访次数" prop="followCount"/>
      <el-table-column :show-overflow-tooltip="true" align="center" label="随访模板描述" prop="followTempDesc"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['research:followTemp:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['research:followTemp:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改随访模板 对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="随访模板名称" prop="followTempName">
          <el-input v-model="form.followTempName" placeholder="请输入随访模板名称"/>
        </el-form-item>
        <el-form-item label="间隔时间" prop="intervalTime">
          <el-row>
            <el-col :span="8">
              <el-input-number v-model="form.intervalTime" :min="1" controls-position="right" style="width: 120px"
              ></el-input-number>
            </el-col>
            <el-col :span="16">
              <el-select v-model="form.intervalUnit" placeholder="请选择时间单位" style="width: 100%; padding-left: 5px">
                <el-option
                  v-for="item in timeType"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key"
                >
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="随访次数" prop="followCount">
          <el-input-number v-model="form.followCount" :min="1" controls-position="right" style="width: 100%"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="随访模板描述" prop="followTempDesc">
          <el-input v-model="form.followTempDesc" :rows="2" placeholder="请输入随访模板描述" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addFollowTemp,
  delFollowTemp,
  getFollowTemp,
  listFollowTemp,
  updateFollowTemp
} from '@/api/research/followTemp'

export default {
  name: 'FollowTemp',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 随访模板 表格数据
      followTempList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        followTempName: undefined
      },
      // 时间单位
      timeType: [
        { key: 'h', value: '时' },
        { key: 'd', value: '天' },
        { key: 'w', value: '周' },
        { key: 'm', value: '月' },
        { key: 'y', value: '年' }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        followTempName: [{ required: true, trigger: 'blur', message: '随访模板名称不能为空' }, {
          max: 30,
          trigger: 'blur',
          message: '随访模板名称不能超过30个字符'
        }],
        intervalTime: { required: true, trigger: 'blur', message: '间隔时间不能为空' },
        intervalUnit: { required: true, trigger: 'change', message: '时间单位不能为空' },
        followCount: { required: true, trigger: 'blur', message: '随访次数不能为空' },
        followDesc: { max: 60, trigger: 'blur', message: '随访模板描述不能超过60个字符' }
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询随访模板 列表 */
    getList() {
      this.loading = true
      listFollowTemp(this.queryParams).then(response => {
        this.followTempList = response.data
        this.loading = false
      })
    },
    // 时间类型翻译
    timeTypeFormat(val) {
      let actions = []
      Object.keys(this.timeType).some((i) => {
        if (this.timeType[i].key === val) {
          actions.push(this.timeType[i].value)
          return true
        }
      })
      return actions.join('')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        followTempId: undefined,
        followTempName: undefined,
        followTempDesc: undefined,
        intervalTime: undefined,
        intervalUnit: undefined,
        followCount: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.followTempId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加随访模板 '
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const followTempId = row.followTempId || this.ids
      getFollowTemp(followTempId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改随访模板 '
      })
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.intervalTime <= 0) {
        this.$message.error('间隔时间必须大于0')
        return false
      }
      if (this.form.followCount <= 0) {
        this.$message.error('随访次数必须大于0')
        return false
      }
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.followTempId != null) {
            updateFollowTemp(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addFollowTemp(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const followTempIds = row.followTempId || this.ids
      this.$confirm('是否确认删除随访模板 编号为"' + followTempIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delFollowTemp(followTempIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
